using System;
using System.Collections.Generic;
using Agatha.Common;
using Common.Logging;
using Source.Common;

namespace  Source.Web.FrameWork
{
    public interface IController : IDisposable { }

    public abstract class Controller<T> : Disposable, IController where T : IView
    {
        private ILog logger = LogManager.GetLogger("Controller");

        protected Controller(T view)
        {
            View = view;
        }
    
    
        protected T View { get; private set; }

        // the following dependencies will be set by the IoC container automatically without having to add it to each derived class' constructor
        public INavigator Navigator { get; set; }
       // public IClientSideUserManager UserManager { get; set; }
        public ILog Loger
        {
            get { return logger; }
            set { logger = value; }
        }
        public IUtilityHelper UtilityHelper { get; set; }
        public IClientSideUserManager UserManager { get; set; }
        private IRequestDispatcher dispatcher;

        public IRequestDispatcher Dispatcher
        {
            get { return dispatcher; }
            set
            {
                dispatcher = value;
            }
        }

        protected override void DisposeManagedResources()
        {
            if (Dispatcher != null) Dispatcher.Dispose();
        }

        protected bool HandlePossibleErrors(Response response)
        {
            return HandlePossibleErrors(response, false);
        }

        protected bool HandlePossibleErrors(Response response, bool rethrowException)
        {
            if (response.Exception != null)
            {
                if(rethrowException)
                {
                    throw new Exception(new TextRepositoryManager().TryGetValue(response.Exception.Message));
                }

                View.DisplayErrorMessages(response.Exception.Message);
                return true;
            }

            return false;            
        }

        protected bool HandlePossibleErrors(IEnumerable<Response> responses)
        {
            return HandlePossibleErrors(responses, false);
        }

        protected bool HandlePossibleErrors(IEnumerable<Response> responses, bool rethrowFirstEncounteredException)
        {
            bool errorsOccured = false;

            foreach (Response response in responses)
            {
                errorsOccured = errorsOccured || HandlePossibleErrors(response, rethrowFirstEncounteredException);
            }

            return errorsOccured;
        }
       
        protected bool HandlePossibleValidationMessages(ResponseWithValidationSummary response)
        {
            if(response.ValidationSummary.HasMessages)
            {
                View.DisplayValidationMessages(response.ValidationSummary);
                return true;
            }

            return false;
        }

    }
}